# -*- coding: utf-8 -*-

from django.db import models

# Create your models here.

class PPCaseInfo(models.Model):
    #开始，刚刚接到单子，但未做处理
    INITIATED = 0
    # 调查中,查询订单，看信息，等待下一步的处理
    ON_VESTIGATION = 1
    # 需要同事处理
    WAITING = 2
    #处理结束
    PROCESS_END = 3

    statu_translation = (
        "开始",
        "调查中",
        "等待同事回应",
        "处理结束",
    )
    caseID = models.CharField(
        max_length = 20,
        primary_key=True,
    )

    orderNo = models.CharField(
        max_length= 25,
        blank = True,
        null= True,
    )
    paymentAmount = models.DecimalField(
        max_digits=9,
        decimal_places=2,
    )
    paymentCurrency = models.CharField(
        max_length=3,
    )
    transactionID = models.CharField(
        max_length=17,
    )
    caseStartDate = models.DateField(
        verbose_name='Case开始日期',
    )
    paypalCaseDeclaration = models.CharField(
        verbose_name='顾客的声明类型',
        max_length=20,
        null=True,
        blank=True,
    )
    paypalCaseStatus = models.CharField(
        verbose_name='PayPal纠纷的状态：争议 或者 补偿请求',
        max_length=8,
        null=True,
        blank=True,
    )

    #indicates that whether PayPal has closed/resolved this dispute
    paypalCaseWindow = models.IntegerField(
        verbose_name='PayPal是否关闭此纠纷',
        max_length=1,
        null=True,
        blank=True,
    )

    # I will turn it into False after click a button name on it
    #indicates that what status the dispute stay on,
    # each status can connect with one action , for example contact
    # customer service with Email, and send this order into an queue wait till limit date come.
    processStatus = models.IntegerField(
        verbose_name='Case处理状态',
        max_length=2,
        null=True,
        blank=True,
    )
    #whether PayPal update it, so we need to check it
    #as admin don't have False option to select, it's impossible to edit
    # PPCaseInfo if don't click the PayPal refresh status into True
    isPayPalUpdate = models.BooleanField(
    )
    paypalRefreshDate = models.DateField(
        verbose_name='PayPal更新此纠纷的时间',
        null=True,
        blank=True,
    )

    def get_absolute_url(self):
        return ('/pp_case/%s/' % self.caseID)
    def know_the_update(self):
        self.isPayPalUptade = False
    
    def __unicode__(self):
        return u"%s" % self.caseID
    class Meta:
        ordering = ['-caseStartDate']


class PPCaseInvestigation(models.Model):
    """
    PayPal case investigation information table
    """
    """
    Here are some choice table for the department or unit should be
    responsible for dispute
    """
    PROVIDER_MANAGEMENT = 1
    CUSTOMER_SERVICE = 2
    CUSTOMER = 3
    LOGISTICS = 4
    IMPROVEMENT_NEEDED=5
    PAYPAL_FRAUD = 6
    MERCHANDISE = 7


    DEPARTMENT_CHOICES = (
        (PROVIDER_MANAGEMENT,"供应商管理"),
        (CUSTOMER_SERVICE,"客服"),
        (CUSTOMER,"客户"),
        (LOGISTICS,"物流"),
        (IMPROVEMENT_NEEDED,"需改进服务"),
        (PAYPAL_FRAUD,"PayPal反欺诈"),
        (MERCHANDISE,"产品"),
    )

    orderNo = models.ForeignKey(PPCaseInfo,
        verbose_name='订单号'
    )
    departmentGroup = models.IntegerField(
        verbose_name='责任部门',
        choices=DEPARTMENT_CHOICES,
        null=True,
        blank=True,
    )
    investigationInfo = models.TextField(
        verbose_name='调查简要',
        null=True,
        blank=True,
    )
    reasonGroup = models.CharField(
        verbose_name='理由分类',
        max_length = 25,
        null=True,
        blank=True,
    )
    involvedProvider = models.CharField(
        verbose_name='涉及供应商',
        max_length=25,
        null=True,
        blank=True,
    )
    involvedCategory = models.CharField(
        verbose_name='涉及品类',
        max_length = 25,
        null=True,
        blank=True,
    )
    def __unicode__(self):
        return u"%s" % self.orderNo

class PPCaseHandle(models.Model):
    ACTION_CHOICES=(
        ('Refund',"全额退款"),
        ('Partial_Refund',"部分退款"),
        ('UploadInfo','提交信息'),
        ('ContactCS','联系客服'),
        ('ContactLS','联系物流'),
        ('ReplyToPP','在PayPal上给客户留言'),
        ('PayPalForceRefund','PayPal强制退款'),
        ('EscalateAndReturn','升级事件要求客人退回')
    )
    caseID = models.ForeignKey(PPCaseInfo)
    handleDate = models.DateField(
        auto_now=True,
    )
    #need to change this field into choices
    handleAction = models.CharField(
        verbose_name="本次采取的行动",
        max_length= 20,
        choices=ACTION_CHOICES,
    )

class PPCaseRefund(models.Model):
    orderNo = models.ForeignKey(PPCaseInfo)
    # no need to add a date on it 
    #refundDate = models.DateField(verbose_name=None)
    refundAmount = models.DecimalField(
        max_digits=9,
        decimal_places=2,
        )
    
class Cooperate(models.Model):
    CONTACT_CHOICES=(
        ('CS','客服'),
        ('Logistics','物流'),
    )
    COLLEAGUES_CHOICES=(
        ('zhangyue@milanoo.cn','张月'),
        ('liuchuqiao@milanoo.cn','测试'),
        ('yangqian@milanoo.cn','杨倩'),
        ('liujingjing@milanoo.cn','刘晶晶'),
        ('gvoinda@milanoo.cn','高文达'),
        ('lili-duo@milanoo.cn','李丽'),
    )
    caseID = models.ForeignKey(PPCaseInfo)
    cooperateDepartment=models.CharField(
        verbose_name='协助部门',
        max_length=20,
        choices=CONTACT_CHOICES,
    )
    requestStaff = models.CharField(
        verbose_name='协助人',
        max_length=50,
        choices=COLLEAGUES_CHOICES,
    )
    requestDate = models.DateTimeField(
        verbose_name='联系的时间',
        null = True,
        blank=True,
    )
    deadline = models.DateField(
        verbose_name='要求回复时间',
        null = True,
        blank= True,
    )
    expectSolution = models.TextField(
        verbose_name='期待同事达到的效果',
    )
    settledDate =models.DateField(
        verbose_name='同事回复解决方案时间',
        null=True,
        blank=True,
    )
    repliedSolution = models.TextField(
        verbose_name='实际回复情况',
        null=True,
        blank=True,
    )
    class Meta:
        ordering = ['deadline']

class CaseResult(models.Model):
    CHICES_RESULT = (
        ('Lose','被扣款'),
        ('InFavor','仲裁对我们有利'),
        ('AgreePRefund','同意部分退款'),
        ('RefundClose','退款结束了事件'),
        ('CloseByCust','客人取消投诉')
    )
    caseID = models.ForeignKey(PPCaseInfo)
    endDate = models.DateField(
        verbose_name='投诉/纠纷结束时间',
    )
    result = models.CharField(
        verbose_name='投诉/纠纷结束的结果',
        max_length='20',
        choices=CHICES_RESULT,
    )